//
//  RightTableViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 fcx. All rights reserved.
//
#import "RightTableViewController.h"

@interface RightTableViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *rightTableView;
@property (nonatomic, strong) NSMutableArray *rightArray;

@property (nonatomic, strong) MJRefreshNormalHeader *header;
@property (nonatomic, assign) BOOL isUpLoading;
@property (nonatomic, assign) NSInteger page;

@end

@implementation RightTableViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.rightArray = [NSMutableArray array];//初始化数组

    self.rightTableView = [UITableView new];
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.scrollsToTop = YES;//快速回到顶部
    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell线
    [self.rightTableView registerClass:[RightTableViewCell class] forCellReuseIdentifier:@"cells"];
    [self.view addSubview:self.rightTableView];

    [self.rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.view);
    }];

    [self addHeaderRefresh];
    [self addFooterRefresh];
}

#pragma mark -- 刷新
- (void)addHeaderRefresh{

    self.rightTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        self.isUpLoading = NO;

        self.page = 0;

        [self getAFNetWorkingData];
    }];

    [self.rightTableView.header beginRefreshing];
}

#pragma mark -- 加载
- (void)addFooterRefresh{

    self.rightTableView.footer  = [MJRefreshAutoFooter footerWithRefreshingBlock:^{

        self.isUpLoading = YES;

        self.page += 2;

        [self getAFNetWorkingData];
    }];
}

#pragma mark -- 网络请求
- (void)getAFNetWorkingData{

    NSString *url = [NSString stringWithFormat: @"http://chanyouji.com/api/articles.json?page=%ld", self.page];

    [AppTools getDataWithUrlStr:url andBlock:^(id result) {

        if (!self.isUpLoading) {
            [self.rightArray removeAllObjects];
        }

        for (NSMutableDictionary *dic in result) {

            RightModel *model = [RightModel modelWithDictionary:dic];

            [self.rightArray addObject :model];
        }

        [self.rightTableView.header endRefreshing];//结束下拉
        [self.rightTableView.footer endRefreshing];//结束加载
        [self.rightTableView reloadData];//刷新
    }];
}

#pragma mark -- 行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.rightArray.count;
}

#pragma mark -- cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *travelsRightcell = @"cells";

    RightTableViewCell *rightCell = [tableView dequeueReusableCellWithIdentifier:travelsRightcell];

    rightCell.selectionStyle = UITableViewCellSelectionStyleNone;

    RightModel *rightModel = [self.rightArray objectAtIndex:indexPath.row];

    rightCell.rightModel = rightModel;

    return rightCell;
}

#pragma mark -- 行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat{

    return 200;
}

#pragma mark -- 触摸
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];//点中置灰  放开取消置灰

    RightDetailTableViewController *rightDetailView = [RightDetailTableViewController new];

    RightModel *rightModels  = [self.rightArray objectAtIndex:indexPath.row];

    rightDetailView.pageId = [rightModels pId];//传入二级页面id

    rightDetailView.name = [rightModels name];//二级页面名字

    rightDetailView.images = [rightModels image_url];//二级页面图片

    rightDetailView.titlels = [rightModels title];//二级页面标题

    rightDetailView.hidesBottomBarWhenPushed = YES;//隐藏tabbar

    [self.navigationController pushViewController:rightDetailView animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
